package com.yuedou.api.controller.platform;

import cn.hutool.json.JSONObject;
import com.yuedou.api.constant.ResponseCode;
import com.yuedou.api.service.platfom.AdBannerService;
import com.yuedou.api.service.sys.SysNoticeService;
import com.yuedou.api.service.titlemenu.TitleMenuService;
import com.yuedou.api.util.GsonResultBean;
import com.yuedou.api.util.Tools;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *@Author: ZhouWei
 *@Description: Redis 重置請求
 *@Date 2025-01-17
 */
@Component
@RestController
@RequestMapping("/redis/reset")//url:模块/资源/{}/细分
public class RedisResetController extends BaseController {
    @Resource
    private AdBannerService adBannerService;
    @Resource
    private TitleMenuService titleMenuService;
    @Resource
    private SysNoticeService sysNoticeService;


    /**
     * 重置banner 数据
     * @param
     * @throws Exception
     */
    @GetMapping(value="/banner/{type}")
    public JSONObject resetBanner(@PathVariable("type") Integer type){
        JSONObject result = null;
        try{
            adBannerService.resetBannerData(type);
            return Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功"),true,true);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 重置 菜单 数据
     * @param
     * @throws Exception
     */
    @GetMapping(value="/menu/{type}")
    public JSONObject resetMenu(@PathVariable("type") Integer type){
        JSONObject result = null;
        try{
            System.out.println("清除缓存 menu：" + type);
            titleMenuService.resetMenuData(type);
            titleMenuService.resetMenuDataSpecial("special");
            return Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功"),true,true);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 重置 菜单 数据
     * @param
     * @throws Exception
     */
    @GetMapping(value="/title/menu/{type}")
    public JSONObject resetMenu(@PathVariable("type") String serviceType){
        JSONObject result = null;
        try{
            System.out.println("清除缓存 title menu：" + serviceType);
            titleMenuService.resetMenuDataSpecial(serviceType);
            return Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功"),true,true);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 重置 通知公告 数据
     * @param
     * @throws Exception
     */
    @GetMapping(value="/notice")
    public JSONObject resetNotice(){
        JSONObject result = null;
        try{
            sysNoticeService.resetDate();
            return Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功"),true,true);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
            e.printStackTrace();
        }
        return result;
    }
}
